using System;
using System.Collections.Generic;

namespace nToolbox
{
    public class MathHelper
    {
        /// <summary>
        /// Calculates the median value of a list of integers
        /// </summary>
        /// <param name="values"></param>
        /// <returns></returns>
        public static decimal MedianValue(List<int> values)
        {
            if (values.Count == 0)
                return 0;
            else if (values.Count == 1)
                return values[0];
            else
            {
                values.Sort();

                if (values.Count % 2 != 0)
                {
                    //get the middle number
                    decimal pos = values.Count / 2;
                    Math.Truncate(pos);
                    return (decimal)values[(int)pos];

                }
                else
                {
                    int pos = values.Count / 2;
                    decimal result = ((decimal)values[pos] + (decimal)values[pos - 1]) / 2;
                    return result;

                }
            }
        }
    }
}
